﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QUAN_LY_MUON_TRA_SACH
{
    public partial class frmphatdocgia : Form
    {
        public frmphatdocgia()
        {
            InitializeComponent();
        }

        private void calendarCombo1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void frmphatdocgia_Load(object sender, EventArgs e)
        {
            try
            {
                //load du lieu len cbo mathe
                cbomathe.Enabled = true;
                DataTable bang = new DataTable();
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("select mathe,hten_dg from the_doc_gia", Connection.sqlConnection);
                sqlDataAdapter.Fill(bang);
                cbomathe.DataSource = bang;
                cbomathe.ValueMember = "mathe";
                sqlDataAdapter.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        internal void btnluu_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand lenh = new SqlCommand("select stt_phieu from phieu_muon where mathe='" + cbomathe.Text + "' and ngay_gio_muon=(select max(ngay_gio_muon) from phieu_muon where mathe='" + cbomathe.Text + "')", Connection.sqlConnection);
                int n = Convert.ToInt32(lenh.ExecuteScalar());

                lenh = new SqlCommand("insert into phieu_phat(msnv,stt_phieu,ngaylap,sotien_phat,ly_do) values('" + FORM_MAIN.frmmain.barmsnv.Caption + "'," + n + ",'" + cadngaylap.Text + "'," + Convert.ToDouble(txtsotien.Text) + ",N'" + txtlydo.Text + "')", Connection.sqlConnection);
                lenh.ExecuteNonQuery();
                if (MessageBox.Show("Đã lập phiếu phạt đọc giả!\nBạn có muốn in phiếu phạt cho đọc giả?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btninphieu_Click(sender,e);
                }
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: "+ex.Message);
            }
        }

        public void btninphieu_Click(object sender, EventArgs e)
        {
            mathe = cbomathe.Text;
            frmphieu_phat phieuphat = new frmphieu_phat();
            phieuphat.ShowDialog();
            
        }
    }
}
